package com.itheima.web;

import com.itheima.support.SysConst;
import com.itheima.utils.FileUtils;
import com.itheima.utils.RedisUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
public class UploadController {

//    @Resource
//    private RedisTemplate<String,Object> redisTemplate;

    /**
     *文件上传
     * */
    @PostMapping("/upload")
    public String upload(@RequestParam("myfile") MultipartFile multipartFile) throws IOException {
        //multipartFile.getOriginalFilename() 上传的文件名
        //multipartFile.getInputStream() 上传的文件流
        String path = FileUtils.upload(multipartFile.getOriginalFilename(),multipartFile.getInputStream());
        //将path存放到file:allset
//        redisTemplate.opsForSet().add("file:allset",path);
        RedisUtil.addToSet(SysConst.SETMEAL_ALL_SET,path);
        //对file:{path}设置有效时间
//        redisTemplate.opsForValue().set("file:" + path,1,30L, TimeUnit.SECONDS);
        //设置1分钟有效
        RedisUtil.set(SysConst.SETMEAL_BASE_FILE + path,1,1L);
        return path;
    }

}
